package controlador;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FrontController extends HttpServlet {

    private String vistas[] = {"indice", "factura", "pago", "cliente", "reportes"};
    private String acciones[] = {"nuevo", "editar", "listado", "detalle", "menu"};

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * v: 0.-index 1.-factura 2.- pago 3.-cliente 4.-reportes
     * 
     * ac: 0.-nuevo 1.-editar 2.-listado 3.-detalle 4.-menu
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=ISO-8859-1");
        PrintWriter out = response.getWriter();
        if (request.getSession().getAttribute("sUsuario") == null) {
            request.getSession().invalidate();
            response.sendRedirect(getServletContext().getContextPath());
        }
        try {
            int vista, accion;
            if (request.getSession().getAttribute("sUsuario") == null) {
                vista = 4;
                accion = 4;
            } else {
                vista = request.getParameter("v") == null ? -1 : Integer.parseInt(request.getParameter("v"));
                accion = request.getParameter("ac") == null ? -1 : Integer.parseInt(request.getParameter("ac"));
            }
            String url = "/jsp/" + vistas[vista] + "/" + acciones[accion] + ".jsp";
            out.println(url);
            getServletContext().getRequestDispatcher(url).forward(request, response);
        } catch (Exception e) {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Error - Front Controller</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("Error al cargar la pagina solicitada");
            out.println("<br/><br/><br/>" + e.toString() + "<br/>" + e.getMessage());
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

// <editor-fold defaultstate="collapsed" desc="Metodos HttpServlet. Click en el signo + izquierdo para editar el codigo.">
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
// </editor-fold>
}
